<!-- 版本 -->
<div class="modal fade" id="create-versions-modal">
    <div class="modal-dialog" role="document">
       <div class="modal-content">
           <div class="modal-header">
               <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
               <span style="font-size: 20px;"><span id="referralname">添加</span></span>
           </div>
           <div class="modal-body">
               <div data-bind="visible: loading" class="loading-panel" style="display: none;">
                   <i class="fa fa-spin fa-spinner"></i>
               </div>
               <form class="form-horizontal modelform">
                    <div class="form-group">
                        <label class="control-label col-sm-2">评级</label>
                        <div class="col-lg-9">
                            <input type="text" class="form-control">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="control-label col-sm-2">上传文本</label>
                        <div class="col-lg-9">
                                <button type="button" class="btn btn-primary" id="picker_ebook11" >
                                    上传
                                </button>
                        </div>
                    </div>
                    
               </form>
           </div>
           <div class="modal-footer">
               <button type="button" id="createreferral" class="btn btn-primary ">提交</button>
           </div>
       </div>
   </div>
</div>
<script>
    // 版本 查看
    $('.verTable .look').click(function () {
        goVerUrl({
            replace_id: $(this).data('id'),
        })
    })
    // 版本 分页
    layui.use(['laypage', 'layer'], function () {
        var laypage = layui.laypage,
            layer = layui.layer;
        // 版本
        laypage.render({
            elem: 'verPage',
            count: '{$repPage.repTotal}',
            theme: '#1E9FFF',
            limit: '{$repPage.repLimit}',
            curr: '{$repPage.repPage}',
            jump: function (obj, first) {
                if (!first) {
                    goVerUrl({
                        repPage:obj.curr
                    })
                }
            }
        });
    })
    function goVerUrl(params) {
        function objectToQuery() {
            let obj = arguments[0];
            let prefix = arguments[1];
            if (typeof obj !== "object") return "";
            const attrs = Object.keys(obj);
            return attrs.reduce((query, attr, index) => {
                // 判断是否是第一层第一个循环
                if (index === 0 && !prefix) query += "?";
                if (typeof obj[attr] === "object") {
                    const subPrefix = prefix ? `${prefix}[${attr}]` : attr;
                    query += this.objectToQuery(obj[attr], subPrefix);
                } else {
                    if (prefix) {
                        query += `${prefix}[${attr}]=${obj[attr]}`;
                    } else {
                        query += `${attr}=${obj[attr]}`;
                    }
                }
                // 判断是否是第一层最后一个循环
                if (index !== attrs.length - 1) query += "&";
                return query;
            }, "");
        }
        let {
            replace_id = '{$replace_id}', //版本id
            repPage = '{$repPage.repPage}', //分页
        } = params
        window.location.href = objectToQuery({
            replace_id,
            repPage
        })
    }

    // 版本 选用 评级
	$('.verTable #score').change(function(){
		bookChoose({
			id:$(this).data('id'),
			score:$(this).val(),
		})
	})
	$('.verTable #choose').change(function(){
		bookChoose({
			id:$(this).data('id'),
			choose:$(this).val(),
		})
	})
	function bookChoose(data){
		$.ajax({
			type: "POST",
			url:"{:url('book/versionEdit')}",
			data,
			dataType: "json",
			success: function(data){
				if(data.code==1){
					updateAlert(data.msg, 'success');
					setTimeout(function() {	
						window.location.reload();
					}, 100);
				}else{
					updateAlert("修改"+data.msg, 'danger');
				}
			},
		})
	}
</script>